//
//  SystemInlineViewController.m
//  NewApp
//
//  Created by  on 12-6-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SystemInlineViewController.h"
#import "NAImageView.h"
#import "AppDelegate.h"

@implementation SystemInlineViewController
@synthesize ID;
@synthesize titleText;
@synthesize imageView;
@synthesize textView;
@synthesize scrollView;

- (id) init{
    
    if (self == NULL) {
        [super init];
        ID = @"";
    }
    
    return self;

}

- (void)viewDidLoad {
    [super viewDidLoad];
	
    //Build UI
    [self buildUserInterface];

    //Load request
    [self loadInlineInfos];
}

- (void) loadInlineInfos {
    //Send request
    if (ID == NULL || [ID isEqualToString:@""]) {
        requestType = REQUEST_TYPE_SYSTEMLINE;
        [self.engine systeminlineInfos]; 
    }else {
        requestType = REQUEST_TYPE_DETAIL;
        [self.engine detailInfos:ID];
        
    }
}

- (void) dealloc {
    [ID release];
    [titleText release];
    [textView release];
    [imageView release];
    [scrollView release];
    [super dealloc];
}

- (void) buildUserInterface {
    
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(0.f, 25.f, 320.f, 460.f - 25.f - 49.f - 44.f)];
    self.scrollView = sv;
    [sv release];
    
    //Build image view
    NATextImageView *v = [[NATextImageView alloc] initWithFrame:CGRectMake(10.f, 10.f, 300.f, 225.f)];
    self.imageView = v;
    [v release];
    [self.scrollView addSubview:self.imageView];
    
    //Build text view
    UITextView *tv = [[UITextView alloc] initWithFrame:CGRectMake(10.f, 240.f, 300.f, 115.f)];
    tv.backgroundColor = [UIColor clearColor];
    tv.editable = NO;
    tv.showsVerticalScrollIndicator = NO;
    tv.showsHorizontalScrollIndicator = NO;
    tv.textColor = [UIColor whiteColor];
    self.textView = tv;
    [tv release];
    self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width, self.textView.contentSize.height);
    self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.textView.frame.origin.y + self.textView.frame.size.height);
    [self.scrollView addSubview:self.textView];
    
    [self.view addSubview:self.scrollView];
}

- (void) requestDidFinished:(RequestEngine *)reqEngine withObject:(id)object {
    
    if ([object isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dic = (NSDictionary *) object;
        [dic retain];
        
        //Retrieve text
        NSString *topTitle = [dic objectForKey:@"top_title"];
        if (topTitle != nil && topTitle.length > 0) { 
            self.titleLabel.text  = topTitle;
            //self.title = topTitle;
        }
        
        //Retrieve image
        NSDictionary *dataDic = [[dic objectForKey:@"data"] objectAtIndex:0];
        if (dataDic != nil) {
            NSString *imageURL = [dataDic objectForKey:@"img"];
            if (imageURL != nil && imageURL.length > 0) {
                UIImage *image = [g_imageLoader cachedImageForURL:[NSURL URLWithString:imageURL] target:self.imageView];
                if (image != nil) {
                    [self.imageView didLoadImage:[NSURL URLWithString:imageURL] Image:image];
                }
            }
            
            //Retrieve info
            NSString *info = [dataDic objectForKey:@"info"];
            if (info != nil && info.length > 0) {
                self.textView.text = info;
                self.textView.font = [UIFont systemFontOfSize:14.0f];
                self.textView.frame = CGRectMake(self.textView.frame.origin.x, self.textView.frame.origin.y, self.textView.frame.size.width, self.textView.contentSize.height);
                self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, self.textView.frame.origin.y + self.textView.frame.size.height);
            }
        }
        

        
    }
}


@end
